﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;

using AjaxPro;

namespace ECCIT.IO
{
    [Serializable]
    [AjaxNamespace("ExcelHtmlFileReader")]
    public class ExcelHtmlFileReader : IDisposable
    {
        public TextReader Reader { get; set; }

        public ExcelHtmlFileReader () { }

        public ExcelHtmlFileReader(string filePath)
        {
            if (!File.Exists(filePath))
                throw new FileNotFoundException();

            this.Reader = new StreamReader(filePath, Encoding.GetEncoding("gb2312"));
        }

        public void Close()
        {
            if (this.Reader != null)
            {
                this.Reader.Close();
                this.Reader.Dispose();
            }
        }

        public HtmlTable GetHtmlTable()
        {
            string line = null;
            HtmlTable table = null;
            while ((line = Reader.ReadLine()) != null)
            {
                if (line.ToLower().StartsWith("<table"))
                {
                    table = new HtmlTable();
                    break;
                }
            }

            if (table != null)
                AddDataRows(table);

            return table;
        }

        private void AddDataRows(HtmlTable table)
        {
            string line = null;
            string lowerLine = null;
            HtmlTableRow row = null;
            bool cellNotEnd = false;
            string cellHtml = "";
            while ((line = Reader.ReadLine()) != null)
            {
                lowerLine = line.ToLower();
                if (lowerLine.TrimStart().StartsWith("<tr"))
                {
                    if (lowerLine.IndexOf("display:none") == -1)
                    {
                        row = new HtmlTableRow();
                        table.Rows.Add(row);
                    }
                }
                else if (lowerLine.TrimStart().StartsWith("<td") || cellNotEnd)
                {
                    cellHtml += line;

                    if (!lowerLine.TrimEnd().EndsWith("</td>"))
                    {
                        cellNotEnd = true;
                        continue;
                    }

                    HtmlTableCell cell = new HtmlTableCell();
                    cell.InnerText = ParseTableCellInnerText(cellHtml);
                    cellHtml = "";
                    cellNotEnd = false;
                    if (row != null)
                        row.Cells.Add(cell);
                }

            }
        }

        private string ParseTableCellInnerText(string cellHtml)
        {
            /*
             * here is the sample of cellHtml:
             * <td class=xl28 x:num="513.20600000000002">513.206</td>
             * */
            if (!cellHtml.ToLower().TrimStart().StartsWith("<td") || !cellHtml.ToLower().TrimEnd().EndsWith("</td>"))
                return string.Empty;

            int rightAngleBracketsPos = cellHtml.IndexOf(">", 0);
            if (rightAngleBracketsPos == -1)
                return string.Empty;

            int leftAngleBracketsPos = cellHtml.IndexOf("<", rightAngleBracketsPos);
            if (leftAngleBracketsPos == -1)
                return string.Empty;

            return cellHtml.Substring(rightAngleBracketsPos + 1, leftAngleBracketsPos - rightAngleBracketsPos - 1);
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.Close();
        }

        #endregion
    }
}
